package bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;

import dao.JPA;
import dao.TreatmentRequestDAO;
import entity.TreatmentRequest;

@ManagedBean
@ViewScoped
public class ViewTreatmentRequestBean {
	
	@Inject @JPA
	private TreatmentRequestDAO requestDAO;
	private TreatmentRequest treatmentRequest = new TreatmentRequest();	
	private int reqId;
	private List<Integer> hourList = new ArrayList<>();
	private List<Integer> minuteList = new ArrayList<>();
	@NotNull(message="You must set meet time")
	private Integer selectedHour;
	@NotNull(message="You must set meet time")
	private Integer selectedMinute;

	@PostConstruct
	private void init() {		
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();		
		reqId = Integer.valueOf(params.get("reqId"));
		treatmentRequest = requestDAO.getEntityById(reqId);
		for(int i = 8; i < 18; i++)
			hourList.add(i);
		for(int i = 0; i < 60; i += 15) 			
			minuteList.add(i);
		selectedHour = treatmentRequest.getMeetDate().get(Calendar.HOUR_OF_DAY);
		selectedMinute = treatmentRequest.getMeetDate().get(Calendar.MINUTE);
	}
	
	public void updateRequest() {			
		requestDAO.merge(treatmentRequest);
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Request updated", "Request updated"));
	}
	
	public void deleteRequest() {
		requestDAO.remove(treatmentRequest);
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Request deleted", "Request deleted"));
		goBack();
	}

	public TreatmentRequest getTreatmentRequest() {
		return treatmentRequest;
	}

	public void setTreatmentRequest(TreatmentRequest treatmentRequest) {
		this.treatmentRequest = treatmentRequest;
	}
	
	public void goBack() {
		try {
			if(FacesContext.getCurrentInstance().getExternalContext().isUserInRole("Receptionist"))
				FacesContext.getCurrentInstance().getExternalContext().redirect("../rec/viewTreatmentRequestList.xhtml");
			else FacesContext.getCurrentInstance().getExternalContext().redirect("viewTreatmentRequestList.xhtml");
		} catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public int getReqId() {
		return reqId;
	}

	public List<Integer> getHourList() {
		return hourList;
	}

	public List<Integer> getMinuteList() {
		return minuteList;
	}

	public Integer getSelectedHour() {
		return selectedHour;
	}

	public Integer getSelectedMinute() {
		return selectedMinute;
	}

	public void setSelectedHour(Integer selectedHour) {
		this.selectedHour = selectedHour;
		treatmentRequest.getMeetDate().set(Calendar.HOUR_OF_DAY, selectedHour);
	}

	public void setSelectedMinute(Integer selectedMinute) {
		this.selectedMinute = selectedMinute;
		treatmentRequest.getMeetDate().set(Calendar.MINUTE, selectedMinute);
	}
	
}
